package Data;

import AppSystem.Settinger;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public  class BitmapRequest {

    public static Bitmap ScaleImage(Bitmap orgin)
    {
        Matrix matrix = new Matrix();
        matrix.setScale(Settinger.getImageScale(), Settinger.getImageScale());
        return Bitmap.createBitmap( orgin, 0, 0,  orgin.getWidth(), orgin.getHeight(), matrix, false);
    }

    public static void GetImagePathFromUser(Context context)
    {
        Intent intent=new Intent(Intent.ACTION_GET_CONTENT).setType("image/*").addCategory(Intent.CATEGORY_OPENABLE);
        ((AppCompatActivity)context).startActivityForResult(intent,1);
    }
    static Handler handler;
    public static  void RequestBitmap(Context context, int requestCode, int resultCode, Intent data, IBitmapHandle handle)
    {
            handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                handle.Handle(msg);
                return false;
            }
        });
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程
            //Toast.makeText(EditItemActivity.this,path.getEncodedPath(),Toast.LENGTH_SHORT).show();
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),uri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Message msg=new Message();
            if (bitmap==null)
            {
                msg.what=0;
            }
            else
            {
                msg.what=1;
                msg.obj=bitmap;
            }
            handler.sendMessage(msg);
        }

    }

}
